package org.ssm.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.ssm.entity.Bill;
import org.ssm.entity.User;
import org.ssm.service.UserService;

@Controller
public class UserController {
	
	@Autowired
	private UserService userService;

	 @RequestMapping(value="/findbyId",method=RequestMethod.GET)
	public String findbyId(@RequestParam("id")String id,HttpServletRequest request,Model model){
		 User user = new User();
		 user.setId(id);
		
		 User user1 =  userService.findOne(user);
		 if(user1 != null){
			 String msg = user1.toString();
		     System.out.println(msg);
		     request.setAttribute("msg", msg);
	         model.addAttribute("msg", msg);
	         return "msg";
		 }
		 request.setAttribute("error", "没有找到该用户！");
	     return "error";
	}
	 
	 @RequestMapping(value="/findAll",method=RequestMethod.GET)
		public String findbyAll(HttpServletRequest request,Model model){
			 
			
			 List<User> list =  userService.findAll(null);
			 if(list != null){
				 String msg = list.get(0).getUserName();
			     System.out.println(msg);
			     request.setAttribute("list", list);
		         return "msg";
			 }
			 request.setAttribute("error", "没有找到该用户！");
		     return "error";
		}
	 
	 @RequestMapping(value="/add.do",method=RequestMethod.POST)
		public String add(@ModelAttribute("form") Bill bill){
			 System.out.println("========================");
		     return "error";
		}
}
